﻿using System.Text;
using System.Windows.Forms;

using WindowsLive.Writer.Api;

namespace WindowsLiveWriter.TagGenerator
{
	[InsertableContentSource("Auto Tag Generator", SidebarText = "Auto Generated Tags"),
	WriterPlugin("696F6564-358E-4ac2-9E82-E32F7B954C7C",
		"Auto Tag Generator",
		ImagePath = "Resources.TagIcon.png",
		Description = "SourceCodePlugin is a plug-in to Windows Live Writer to enable code syntax highlighting using Alex Gorbatchev syntax highlighter.",
		PublisherUrl = "http://www.alexjamesbrown.com")]
	public class TagGeneratorPlugin : ContentSource
	{
		public override DialogResult CreateContent(IWin32Window dialogOwner, ref string content)
		{
			DialogResult result = DialogResult.Cancel;

			if ((content != null) && (content != string.Empty))
			{
				using (TagForm form = new TagForm(content))
				{
					result = form.ShowDialog(dialogOwner);
					if (result == DialogResult.OK)
					{
						//generate the tag string
						StringBuilder sb = new StringBuilder();
						foreach (var tag in form.Tags)
							sb.Append(string.Format("{0},", tag));

						//copy to clipboard - remove the last comma ","
						Clipboard.SetText(sb.ToString().TrimEnd(','));

						showMsg("Tags copied to clipboard", MessageBoxIcon.Information);
					}
				}
			}
			else
			{
				showMsg("Please select content first", MessageBoxIcon.Exclamation);
			}

			return result;
		}

		/// <summary>
		/// Shows a message box.
		/// </summary>
		/// <param name="message"></param>
		/// <param name="icon"></param>
		private static void showMsg(string message, MessageBoxIcon icon)
		{
			MessageBox.Show(message, "Tag Generator Plug-In", MessageBoxButtons.OK, icon);
		}
	}
}